package dao

import (
	"morleyStoneHome/model"
)

type newsDao struct {
}

var NewsDao = newNewsDao()

func newNewsDao() *newsDao {
	return &newsDao{}
}

func (n *newsDao) ListAll(lan string) (ns []model.News, err error) {
	//result := Db.Find(&ns)
	//find := Db.Where("language = ?", lan).Find(&ns)
	find := Db.Table("news").Select([]string{"news_id", "title", "ctime", "image"}).Where("language = ?", lan).Scan(&ns)
	if nil != find.Error {
		//logrus.Error(find.Error)
		return nil, find.Error
	}
	return ns, nil
}

func (n *newsDao) FindOne(id int, lan string) (ns []model.News, err error) {
	res := Db.Where("news_id = ? and language = ?", id, lan).Find(&ns)
	if res.Error != nil {
		//logrus.Error(res.Error)
		return nil, res.Error
	}
	return ns, nil
}
